package vn.edu.saigontech.ncms.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.enumeration.AttendanceStatus;
import vn.edu.saigontech.ncms.model.info.AbstractReceipt;
import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.studying.Attendance;
import vn.edu.saigontech.ncms.service.AttendanceService;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.vo.AttendanceReportItem;

@SuppressWarnings("unchecked")
public class AttendanceServiceImpl extends
		HibernateGenericService<Attendance, Long> implements AttendanceService {

	@Override
	public Attendance add(Long offeringMemberId, Date date,
			AttendanceStatus status, String comment) {
		Attendance attendance = new Attendance();
		attendance.setOfferingMember(OfferingMemberService.INSTANCE
				.findById(offeringMemberId));
		attendance.setDate(date);
		attendance.setStatus(status);
		attendance.setComment(comment);
		return super.persist(attendance);
	}

	@Override
	public Attendance edit(Long id, Date date, AttendanceStatus status,
			String comment) {
		Attendance attendance = super.findById(id);
		attendance.setDate(date);
		attendance.setStatus(status);
		attendance.setComment(comment);
		return super.persist(attendance);
	}
	
	@Override
	public boolean deleteAndFlush(Long id) {
		Attendance attendance = super.findById(id);
		boolean ret = super.deleteById(attendance.getId());
		super.getSession().flush();
		return ret;
	}

	@Override
	public Attendance findByOfferingMemberIdAndDate(Long offeringMemberId,
			Date date) {
		Criteria criteria = this.createCriteria();
		criteria.createCriteria("offeringMember").add(
				Restrictions.eq("id", offeringMemberId));
		criteria.add(Restrictions.eq("date", date));
		try {
			return (Attendance) criteria.uniqueResult();
		} catch (Exception ex) {
			List<Attendance> list = criteria.list();
			if (list.size() > 1) {
				Long id = list.get(0).getId();
				for (int i = 1; i < list.size(); i++) {
					this.deleteById(list.get(i).getId());
				}
				return this.findById(id);
			}
		}
		return null;
	}

	@Override
	public Collection<AttendanceReportItem> reportAttendance(Long offeringId) {
		Collection<AttendanceReportItem> result = new ArrayList<AttendanceReportItem>();
		Offering offering = OfferingService.INSTANCE.findById(offeringId);
		Collection<Date> dates = DateUtil.sequence(offering.getStartDate(),
				offering.getEndDate());
		for (Date date : dates) {
			if (DateUtil.getDayOfWeek(date) == Calendar.SUNDAY) {
				for (AbstractReceipt receipt : offering.getOfferingReceipts()) {
					// TODO
					receipt.getId();
				}
			}
		}
		return result;
	}
}